package com.stox.ui.javafx.navigator.widget;

import java.util.Arrays;

import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;

import org.springframework.context.ApplicationEventPublisher;

import com.stox.design.Callback;
import com.stox.event.AbstractModelSelectionEvent;
import com.stox.model.AbstractModel;
import com.stox.ui.javafx.core.widget.wrapper.Button;
import com.stox.ui.javafx.core.widget.wrapper.ImageView;
import com.stox.ui.javafx.core.widget.wrapper.TextField;
import com.stox.ui.javafx.core.widget.wrapper.TreeItem;
import com.stox.ui.javafx.core.widget.wrapper.TreeView;
import com.stox.ui.javafx.core.widget.wrapper.TreeView.DIRECTION;
import com.stox.ui.javafx.core.widget.wrapper.TreeView.START;
import com.stox.ui.javafx.navigator.model.AbstractModelTreeItem;
import com.stox.util.ApplicationContextUtil;

public class SearchPanel extends HBox implements EventHandler<KeyEvent> {
	
	
	private final TreeView<String> treeView;
	private final Button previousButton;
	private final Button nextButton;
	private final Button firstButton;
	private final Button lastButton;
	private final TextField textField;
	
	private final Callback<TreeItem<String>, Void> callback = new Callback<TreeItem<String>, Void>(){
		@Override
		public Void execute(TreeItem<String> treeItem) {
			int row = treeView.getRow(treeItem);
			treeItem.expand();
			treeView.scrollTo(row);
			treeView.getSelectionModel().clearAndSelect(row);
			treeView.requestFocus();
			return null;
		}
	};
	
	
	
	public SearchPanel(TreeView<String> treeView) {
		this.treeView = treeView;
		this.textField  = new TextField();
		this.previousButton = new Button(new ImageView("up.png"),treeView.new SearchEventHandler(callback, textField, START.SELECTED, DIRECTION.BACKWARD));
		this.nextButton = new Button(new ImageView("down.png"), treeView.new SearchEventHandler(callback, textField, START.SELECTED, DIRECTION.FORWARD));
		this.firstButton = new Button(new ImageView("double-up.png"), treeView.new SearchEventHandler(callback, textField, START.FIRST, DIRECTION.FORWARD));
		this.lastButton = new Button(new ImageView("double-down.png"), treeView.new SearchEventHandler(callback, textField, START.LAST, DIRECTION.BACKWARD));
		this.treeView.setOnKeyPressed(this);
		this.textField.setOnKeyPressed(this);
		this.getChildren().addAll(firstButton, previousButton, textField, nextButton, lastButton);
	}
	
	private void publishAbstractModelSelectionEvent() {
		TreeItem<?> treeItem = (TreeItem<?>)treeView.getSelectionModel().getSelectedItem();
		if(treeItem != null && treeItem instanceof AbstractModelTreeItem) {
			ApplicationEventPublisher applicationEventPublisher = ApplicationContextUtil.getApplicationContext().getBean(ApplicationEventPublisher.class);
			AbstractModel abstractModel = ((AbstractModelTreeItem<?>)treeItem).getModel(); 
			applicationEventPublisher.publishEvent(new AbstractModelSelectionEvent<AbstractModel>(Arrays.asList(abstractModel)));
		}
	}
	
	@Override
	public void handle(KeyEvent event) {
		if(event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.TAB) {
			publishAbstractModelSelectionEvent();
		}else {
			String text = (textField.getText()+event.getText()).trim().toUpperCase();
			TreeItem<String> treeItem = (TreeItem<String>)treeView.getSelectionModel().getSelectedItem();
			if(treeItem != null && treeItem.getValue().trim().toUpperCase().contains(text)) return;
			treeItem = treeView.search(text ,START.FIRST, DIRECTION.FORWARD);
			if(treeItem != null) {
				int row = treeView.getRow(treeItem);
				treeItem.expand();
				treeView.scrollTo(row);
				treeView.getSelectionModel().clearAndSelect(row);
			}
		}
	}
	
}
